import { BasicColumn, FormSchema } from '@apps/antd-admin/src/components/Table';
import { airportList } from '@apps/ant-bus/src/views/business/airport/data/api';
import { queryByCompany, queryCompanyInfoByCode } from '@apps/ant-bus/src/views/business/financialManage/applyInvoice/data/api';
import { RenderType } from '@pkg/setting';

export const columns: BasicColumn[] = [
  {
    title: '申请时间',
    dataIndex: 'createTime',
    width: 150,
  },
  {
    title: '申请状态',
    dataIndex: 'invoiceState',
    width: 100,
    dictData: [
      { label: '已提交', value: 0, color: '#585b58' },
      { label: '已开票', value: 1, color: '#09f137' },
      { label: '已作废', value: 2, color: '#f60606' },
      { label: '开票中', value: 3, color: '#f69606' },
      { label: '冲红中', value: 4, color: '#064af6' },
    ],
    renderType: RenderType.DICT,
  },
  {
    title: '会员姓名',
    dataIndex: 'userName',
    width: 120,
  },
  {
    title: '手机号',
    dataIndex: 'mobile',
    width: 120,
  },
  {
    title: '卡号',
    dataIndex: 'vipCardNo',
    width: 150,
  },
  {
    title: '卡状态',
    dataIndex: 'vipCardState',
    width: 120,
    dictData: [
      { label: '待激活', value: 0 },
      { label: '正常', value: 1 },
      { label: '已过期', value: 2 },
      { label: '未开通', value: 3 },
      { label: '未通过', value: 4 },
      { label: '冻结', value: 5 },
      { label: '退卡中', value: 6 },
      { label: '已退卡', value: 7 },
      { label: '已转卡', value: 8 },
      { label: '已转赠', value: 9 },
      { label: '冻结（延时开通）', value: 10 },
    ],
    renderType: RenderType.DICT,
  },
  {
    title: '收款方式',
    dataIndex: 'paywayName',
    width: 160,
  },
  {
    title: '金额',
    dataIndex: 'payAmount',
    width: 120,
  },
  {
    title: '开卡地区',
    dataIndex: 'airportName',
    width: 170,
  },
  {
    title: '销售姓名',
    dataIndex: 'employeeName',
    width: 120,
  },
  {
    title: '开卡日期',
    dataIndex: 'openDate',
    width: 140,
  },
  {
    title: '开票公司全称',
    dataIndex: 'invoiceCompanyInfo',
    align: 'left',
    width: 200,
  },
  {
    title: '额外备注',
    dataIndex: 'otherRemark',
    align: 'left',
    width: 500,
  },
  {
    title: '发票备注',
    dataIndex: 'invoiceRemark',
    align: 'left',
    width: 500,
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'startDate',
    label: '开始日期',
    component: 'DatePicker',
    componentProps: {
      valueFormat: 'YYYY-MM-DD',
      format: 'YYYY-MM-DD',
    },
  },
  {
    field: 'endDate',
    label: '结束日期',
    component: 'DatePicker',
    componentProps: {
      valueFormat: 'YYYY-MM-DD',
      format: 'YYYY-MM-DD',
    },
  },
  {
    field: 'userName',
    label: '姓名',
    component: 'Input',
  },
  {
    field: 'mobile',
    label: '手机号',
    component: 'Input',
  },
  {
    field: 'vipCardNo',
    label: '会员卡号',
    component: 'Input',
  },
  {
    field: 'airportIdArray',
    label: '开卡地',
    component: 'ApiSelect',
    componentProps: {
      api: airportList,
      params: { handlerName: 'busAirportRoleListHandler' },
      labelField: 'airportName',
      valueField: 'airportId',
      showSearch: true,
      immediate: true,
      mode: 'multiple',
    },
  },
  {
    field: 'invoiceState',
    label: '开票状态',
    component: 'Select',
    componentProps: {
      options: [
        { label: '已提交', value: 0 },
        { label: '已开票', value: 1 },
        { label: '已作废', value: 2 },
        { label: '开票中', value: 3 },
        { label: '冲红中', value: 4 },
      ],
    },
  },
  {
    field: 'vipCardState',
    label: '会员卡状态',
    component: 'Select',
    colProps: { span: 4 },
    componentProps: {
      options: [
        { label: '待激活', value: 0 },
        { label: '正常', value: 1 },
        { label: '已过期', value: 2 },
        { label: '未开通', value: 3 },
        { label: '未通过', value: 4 },
        { label: '冻结', value: 5 },
        { label: '退卡中', value: 6 },
        { label: '已退卡', value: 7 },
        { label: '已转卡', value: 8 },
        { label: '已转赠', value: 9 },
        { label: '冻结（延时开通）', value: 10 },
      ],
    },
  },
];

export const invoiceSendEmailFormSchema: FormSchema[] = [
  {
    label: '发票申请Id',
    field: 'applyInvoiceId',
    component: 'Input',
    show: false,
    required: true,
  },
  {
    label: '用户邮箱',
    field: 'userEmail',
    component: 'Input',
    required: true,
  },
];

const updateInvoiceCompanyOptions = async (keyword, formActionType) => {
  const { updateSchema, setFieldsValue } = formActionType;
  console.log('keyword', keyword);
  if (!keyword.trim()) return;
  const items = await queryByCompany({ companyName: keyword });
  console.log('items', items);
  updateSchema({
    field: 'invoiceCompany',
    componentProps: {
      options: items,
      placeholder: '请先在上面输入公司名称',
      showSearch: true,
      fieldNames: { label: 'name', value: 'code' },
      onChange: code => {
        if (code) {
          const data: any = items.find(item => item.code == code);
          if (data) {
            setFieldsValue({ invoiceCompanyInfo: data.name });
          }
          queryCompanyInfoByCode({ code }).then(info => setFieldsValue({ kpCode: info.kpCode }));
        }
      },
    },
  });
};

export const applyFormSchema: FormSchema[] = [
  {
    field: 'userGoodsId',
    label: '用户物品Id',
    component: 'Input',
    required: true,
    show: false,
  },
  {
    field: 'fpType',
    label: '发票类型',
    component: 'Select',
    defaultValue: 1,
    required: true,
    componentProps: ({ formModel, formActionType }) => {
      const { updateSchema, setFieldsValue } = formActionType;
      return {
        options: [
          { label: '企业', value: 1 },
          { label: '个人', value: 2 },
        ],
        onChange: value => {
          if (value == 2) {
            updateSchema({
              field: 'kpCode',
            });
            setFieldsValue({ kpCode: '' });
          }
        },
      };
    },
  },
  {
    field: 'invoiceCompanyInfo',
    label: '公司名称输入',
    component: 'Input',
    ifShow: ({ model }) => model.fpType == 1,
    componentProps: ({ formActionType, formModel }) => {
      return {
        onBlur: async e => {
          await updateInvoiceCompanyOptions(e.target.value, formActionType);
        },
        onPressEnter: async e => {
          await updateInvoiceCompanyOptions(e.target.value, formActionType);
        },
      };
    },
  },
  {
    field: 'userName',
    label: '个人姓名',
    component: 'Input',
    ifShow: ({ model }) => model.fpType == 2,
  },
  {
    field: 'invoiceCompany',
    label: '发票抬头公司',
    component: 'Select',
    ifShow: ({ model }) => model.fpType == 1,
    componentProps: ({ formModel, formActionType }) => ({
      options: [],
      placeholder: '请先在上面输入公司名称',
      showSearch: true,
      fieldNames: { label: 'name', value: 'code' },
      onChange: code => {
        const { setFieldsValue } = formActionType;
        if (code) {
          queryCompanyInfoByCode({ code }).then(info => {
            setFieldsValue({ kpCode: info.kpCode });
          });
        }
      },
    }),
  },
  {
    field: 'kpCode',
    label: '企业税号',
    component: 'Input',
    required: true,
    ifShow: ({ model }) => model.fpType == 1,
    rules: [
      { required: true, message: '请填写企业税号' },
      { pattern: /^[A-Z0-9]{15,20}$/, message: '税号格式不正确' },
    ],
  },
  {
    field: 'userEmail',
    label: '接收邮箱',
    component: 'Input',
    rules: [
      { required: true, message: '请输入邮箱' },
      { type: 'email', message: '邮箱格式不正确' },
    ],
  },
  {
    field: 'kpType',
    label: '开票类型',
    component: 'Select',
    defaultValue: 'pc',
    componentProps: {
      options: [
        { label: '数电普票', value: 'pc' },
        { label: '数电专票', value: 'bs' },
      ],
    },
    rules: [{ required: true, message: '请选择开票类型' }],
  },
  {
    field: 'invoiceRemark',
    label: '发票备注',
    component: 'InputTextArea',
    componentProps: { rows: 3, maxlength: 50 },
  },
  {
    field: 'otherRemark',
    label: '其他备注',
    component: 'InputTextArea',
    componentProps: { rows: 3, maxlength: 250 },
  },
];
